package me.zk.practice1.s;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zhangkai
 * @date 2023/8/9 16:52
 */
public class LS1419 {
    public static void main(String[] args) {
        System.out.println(minNumberOfFrogs("croakcroak"));
    }
    public static int minNumberOfFrogs(String croakOfFrogs) {
        if (croakOfFrogs.length() % 5 !=0){
            return -1;
        }
        int[] cnt = new int[4];
        int res = 0, frogNum = 0;
        Map<Character,Integer> map = new LinkedHashMap<>();
        map.put('c',0);
        map.put('r',1);
        map.put('o',2);
        map.put('a',3);
        map.put('k',4);


        for (int i = 0; i < croakOfFrogs.length(); i++) {
            char ch = croakOfFrogs.charAt(i);
            int t = map.get(ch);
            if (t ==0) {
                cnt[t]++;
                frogNum++;
                if (frogNum > res) {
                    res = frogNum;
                }

            } else {
                if (cnt[t-1]==0) {
                    //错序
                    return -1;
                }
                cnt[t-1]--;
                if (t==4) {
                    //已经叫完了
                    frogNum--;
                } else {
                    cnt[t]++;
                }
            }

        }
        if (frogNum>0){
            return -1;
        }
        return res;
    }
}
